<template>
  <submit-record-table :loading="loading" :table-data="tableData" ref="dataTable"></submit-record-table>
  <submit-record-pagination :total="total" :current-page="currentPage" @page-change="pageChange"></submit-record-pagination>
</template>

<script>
import SubmitRecordTable from "@/components/SubmitRecordTable";
import SubmitRecordPagination from "@/components/SubmitRecordPagination";
import GLOBAL from "@/assets/js/const"
import {getSubmitRecord} from "@/network/submitRecord";

export default {
  name: "SubmitRecord",
  components: {
    SubmitRecordTable,
    SubmitRecordPagination
  },
  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      resultTable: GLOBAL.statusCode,
      languageTable: GLOBAL.languageCode,
      loading: false
    }
  },
  methods: {
    pageChange(newPage) {
      this.currentPage = newPage;
      this.loading = true;
      getSubmitRecord(this.currentPage).then(res => {
        if(res.data.status===0) {
          let tmpTableData = [];
          let records = res.data.data.list;
          this.total = res.data.data.total;
          for(let i = 0;i < records.length;i++) {
            let record = {};
            record.id=records[i].id;
            record.resultCode=records[i].result;
            record.result=GLOBAL.statusCode[records[i].result];
            record.problem=records[i].problemId+'. '+records[i].problemName;
            record.language=this.languageTable[records[i].language];
            record.time=records[i].time+' ms';
            record.memory=records[i].memory.toFixed(2)+" MB";
            record.submitTime=records[i].judgeTime;
            this.tableData.push(record);
            tmpTableData.push(record);
          }
          this.tableData = tmpTableData;
          this.loading = false;
        } else {
          alert(res.data.error);
        }
      }).catch(err => {
        console.log(err);
        this.loading = false;
      });
    }
  },
  created() {
    /*
    res: {
      state 状态
      records 提交记录数组
    }
    */
    this.pageChange(1);
  }
}
</script>

<style scoped>

</style>